﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using TweetSharp;
using CsvHelper;

namespace Microsoft.AzureAD.XPrize.TwittOracle.DataCollection
{
    class VerifiedAccountExtractor
    {
        private TwitterService service;

        public VerifiedAccountExtractor(TwitterService service)
        {
            this.service = service;
        }

        public void getValidAccounts()
        {
            // Output file
            string appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            string outputFile = System.IO.Path.Combine(appDataFolder, "accounts_valid.txt");
            StreamWriter file = new StreamWriter(outputFile);
            var csvWriter = new CsvWriter(file);
            Console.WriteLine(outputFile);

            // Populate the file
            var followers = service.ListFriendsOf("verified", -1);
            int count = 0;
            while (followers != null && followers.NextCursor != null)
            {
                Console.WriteLine(count);
                foreach (var follower in followers)
                {
                    Utils.CsvWrite(csvWriter, follower);
                    count++;
                }
                followers = service.ListFriendsOf("verified", followers.NextCursor ?? 0);
            }

            // Cleanup
            csvWriter.Dispose();
            file.Close();
        }
    }
}
